package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.mapper.CartMapper;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CartServiceImpl implements CartService {
    @Autowired
    private CartMapper cartMapper;

    //增加购物车
    @Override
    public Cart add(Cart cart) {
        //1.判断当前要添加菜品/套餐在购物车中是否已经存在
        //select * from shopping_cart where dish_id = #{菜品id} and userId =#{登录用户id}
        //select * from shopping_cart where setmeal_id = #{套餐id} and userId =#{登录用户id}
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId())
                .eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId())
                .eq(Cart::getUserId, UserHolder.get().getId());
        Cart cartFromDb = cartMapper.selectOne(wrapper);
        if (cartFromDb != null) {
            //执行到这里说明数据库中已经添加了此菜品/套餐(购物车中已经有了)
            //数量加一
            cartFromDb.setNumber(cartFromDb.getNumber() + 1);
            cartMapper.updateById(cartFromDb);
            return cartFromDb;
        } else {
            //如果购物车中没有 向数据库中添加数据
            cart.setUserId(UserHolder.get().getId());
            cart.setNumber(1);
            cart.setCreateTime(new Date());
            cartMapper.insert(cart);
            return cart;
        }

    }

    //查询购物车
    @Override
    public List<Cart> find() {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        return cartMapper.selectList(wrapper);

    }

    //修改购物车
    @Override
    public Cart sub(Cart cart) {
        //1.接收参数 获取当前菜品/套餐的数量
        //select * from shopping_cart
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(cart.getDishId() != null, Cart::getDishId, cart.getDishId())
                .eq(cart.getSetmealId() != null, Cart::getSetmealId, cart.getSetmealId())
                .eq(Cart::getUserId, UserHolder.get().getId());
        Cart cartByDb = cartMapper.selectOne(wrapper);
        if (cartByDb.getNumber() > 1) {
            //如果数量大于1,商品数量-1
            cartByDb.setNumber(cartByDb.getNumber() - 1);
            cartMapper.updateById(cartByDb);
        } else {
            //数量只有1的商品直接从数据表中删除
            cartMapper.deleteById(cartByDb);
        }
        return cartByDb;
    }

    //清空购物车
    @Override
    public void clean() {
        LambdaQueryWrapper<Cart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Cart::getUserId, UserHolder.get().getId());
        cartMapper.delete(wrapper);
    }
}
